package utils;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.cassandra.thrift.Cassandra;
import org.apache.cassandra.thrift.Column;
import org.apache.cassandra.thrift.ColumnParent;
import org.apache.cassandra.thrift.ColumnPath;
import org.apache.cassandra.thrift.ConsistencyLevel;
import org.apache.cassandra.thrift.InvalidRequestException;
import org.apache.cassandra.thrift.NotFoundException;
import org.apache.cassandra.thrift.TimedOutException;
import org.apache.cassandra.thrift.UnavailableException;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
public class DataInserter
{
	public static void main(String[] args)
	throws TException, InvalidRequestException, UnavailableException, UnsupportedEncodingException, NotFoundException, TimedOutException
	{
		String UTF8 = "UTF8"; 
		final ConsistencyLevel CL = ConsistencyLevel.ONE;
		int recordNumber = 40000;
		Logger LOG = Logger.getLogger(DataInserter.class.getName());
		TTransport tr = new TFramedTransport(new TSocket("mymed11.sophia.inria.fr", 9160));
		TProtocol proto = new TBinaryProtocol(tr);
		Cassandra.Client client = new Cassandra.Client(proto);
		tr.open();

		client.set_keyspace("Person");
		String cfName = "Users"; 

		for (int i = 30000; i<recordNumber; i++) {

			ByteBuffer userIDKey = ByteBuffer.wrap(Integer.toString(i).getBytes()); //this is a row key
			long clock = System.currentTimeMillis();

			//create a representation of the Name column  
			ColumnPath colPathName = new ColumnPath(cfName); 
			colPathName.setColumn("first".getBytes(UTF8));  
			ColumnParent cp = new ColumnParent(cfName);

			//insert the name column  
			LOG.log(Level.INFO, "Inserting row for key " + new String(userIDKey.array())); 
			client.insert(userIDKey, cp,  new Column(ByteBuffer.wrap("first".getBytes(UTF8)), ByteBuffer.wrap(("Bill samuel" + Integer.toString(i)).getBytes()), clock), CL);
			client.insert(userIDKey, cp,  new Column(ByteBuffer.wrap("last".getBytes(UTF8)), ByteBuffer.wrap(("Clinton perea" + Integer.toString(i)).getBytes()), clock), CL);
			//insert the Age column
			client.insert(userIDKey, cp,  new Column(ByteBuffer.wrap("age".getBytes(UTF8)),  ByteBuffer.wrap(Integer.toString(69 + i).getBytes()), clock), CL);  
			LOG.log(Level.INFO, "Row insert done.");

		}

		tr.close();  

		LOG.log(Level.INFO, "All done."); }
}